package com.saprk.demoarithmetic.arithmetic;

import java.util.Objects;

/**
 * @author fuqiang
 * @Description
 * @Date 2021-04-12
 */
public class BubbleSort {
    /**
     * 选择排序
     * @author fuqiang
     * @date 2021-04-12 20:02
     * @param arr
     * @return int[]
     */
    public static int[] selection(int[] arr){
        if(Objects.isNull(arr)){
            return null;
        }
        if(1 >= arr.length){
            return arr;
        }
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = i+1; j < arr.length - i; j++) {
                if(arr[i] > arr[j]){
                    int k = arr[i];
                    arr[i] = arr[j];
                    arr[j] = k;
                }
            }
        }
        return arr;
    }
    /**
     * 冒泡排序 O(N^2)
     * @author fuqiang
     * @date 2021-04-12 20:16
     * @param arr
     * @return int[]
     */
    public static int[] bubble(int[] arr){
        if(Objects.isNull(arr)){
            return null;
        }
        if(1 >= arr.length){
            return arr;
        }
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length- 1 - i ; j++) {
                if(arr[j] > arr[j+1]){
                    int k = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = k;
                }
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        int[] arr = {1,3,4,1,9,10};
        int[] bubble = bubble(arr);
        for (int i : bubble) {
            System.out.println(i);
        }
    }
}
